<?php

namespace app\api\controller\cmcx;

use app\api\controller\Base;
use app\admin\model\Cmcx;
use think\Db;

/**
 * 重名查询相关接口
 */
class Cmcxs extends Base
{

    public $com_surnames_list = array(
        1 => ['李', '王', '张', '刘', '陈', '杨', '赵', '黄', '周', '吴'],
        2 => ['徐', '孙', '胡', '朱', '高', '林', '何', '郭', '马', '罗'],
        3 => ['梁', '宋', '郑', '谢', '韩', '唐', '冯', '于', '董', '萧'],
        4 => ['程', '曹', '袁', '邓', '许', '傅', '沈', '曾', '彭', '吕'],
        5 => ['苏', '卢', '蒋', '蔡', '贾', '丁', '魏', '薛', '叶', '阎'],
        6 => ['余', '潘', '杜', '戴', '夏', '钟', '汪', '田', '任', '姜'],
        7 => ['范', '方', '石', '姚', '谭', '廖', '邹', '熊', '金', '陆'],
        8 => ['郝', '孔', '白', '崔', '康', '毛', '邱', '秦', '江', '史'],
        9 => ['顾', '侯', '邵', '孟', '龙', '万', '段', '漕', '钱', '汤'],
        10 => ['尹', '黎', '易', '常', '武', '乔', '贺', '赖', '龚', '文'],
    );

    public $com_names_list = array(
        1 => ['英', '华', '玉', '秀', '文', '兰', '国', '金', '春', '伟', '国庆'],
        2 => ['芳', '鹏', '强', '婷婷', '建军', '建国', '秀莲', '志强', '俊杰'],
        3 => ['杰', '军', '涛', '超', '娟', '勇', '平', '婷', '燕', '艳', '博文'],
        4 => ['敏', '霞', '雪', '子轩', '子萱', '欣怡', '皓轩', '志豪', '致远'],
        5 => ['梦', '昊', '静', '琳', '丽', '亮', '明', '磊', '宇', '文', '玲'],
        6 => ['洋', '鑫', '秀英', '桂英', '玉兰', '秀兰', '玉梅', '雨欣', '亚东'],
        7 => ['可', '然', '子', '浩', '梓', '俊', '轩', '豪', '泽', '思', '轩轩'],
        8 => ['涵', '萱', '怡', '晓凯', '晓玲', '俊凯', '艺馨', '彤', '琪', '卫华'],
        9 => ['悦', '晶', '琴', '晴', '雨', '睿', '源', '倩', '疏影', '敬亭'],
        10 => ['君', '子', '天', '珊', '姗', '琦', '舒', '洛', '凯', '婕', '姗姗'],
    );

    public $surname_str = '赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫经房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阴鬱胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍卻璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居衡步都耿满弘匡国文寇广禄阙东欧殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后荆红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于单于太叔申屠公孙仲孙轩辕令狐钟离宇文长孙慕容鲜于闾丘司徒司空丌官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓跋夹谷宰父谷梁晋楚闫法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况郈有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福';

    public $block_list = '你我他屎尿屁的死哦啦了';

    // 配置
    public function config($v)
    {
        $list = array(
            array(
                'name' => '精品盒子',
                'appid' => 'wx7c0beb2fc324a64e',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/cmcx/ad/jp.png'
            )
        );
        $share = true;
        if ($v == '0.0.0') {
            $share = false;
        }
        $flags = array(
            'share' => $share,
            'list' => $list,
        );
        $this->result('flag', $flags);
    }

    // 查询全部
    public function query()
    {
        $surname = $this->request->post('surname');
        $name = $this->request->post('name');
        $_name = $surname . $name;

        if (strstr($this->surname_str, $surname)) {
            $cc = Cmcx::get(['name' => $_name]);
            if (!$cc) {
                $this->save_new_cmcx($surname, $name);
            }
            $res = $this->handler_result($_name);
        } else {
            $res = $this->handler_result('empty');
            $res['name'] = $_name;
        }

        $this->result('ok', $res);
    }

    // 省级查询
    public function query_province()
    {
        $province = strtolower($this->request->post('province'));
        $surname = $this->request->post('surname');
        $name = $this->request->post('name');
        $_name = $surname . $name;

        if ($province == 'shangxi') {
            $province = 'shanxi';
        }

        if (strstr($this->surname_str, $surname)) {
            $cc = Cmcx::get(['name' => $_name]);
            if (!$cc) {
                $this->save_new_cmcx($surname, $name);
            }
            $res = $this->handler_province_result($_name, $province);
        } else {
            $res['all'] = 0;
            $res['male'] = 0;
            $res['female'] = 0;
        }

        $this->result('ok', $res);
    }

    // 处理返回结果
    public function handler_result($_name)
    {
        $cc = Cmcx::get(['name' => $_name]);
        $data = Db::table('fa_cmcx')->where('name', $_name)->field('guangdong,jiangsu,chongqing,henan,tianjin,shanghai,anhui,shanxi,zhejiang,jilin')->find();
        $f = $this->format_res($data);
        $res['name'] = $cc->name;
        $res['rate'] = $cc->rate;
        $res['all'] = $f['all'];
        $res['list'] = $f['list'];
        return $res;
    }

    // 处理省级返回结果
    public function handler_province_result($_name, $province)
    {
        $data = Cmcx::where(['name' => $_name])->value($province);
        $data = explode(',', $data);
        $res['all'] = $data[0] + $data[1];
        $res['male'] = intval($data[0]);
        $res['female'] = intval($data[1]);
        return $res;
    }

    // 新增数据
    public function save_new_cmcx($surname, $name)
    {
        $_name = $surname . $name;
        $new  = new Cmcx();
        $new->name = $_name;
        $data = $this->handler_name($surname, $name);
        $new->rate = round($data['rate'], 2);
        $this->handler_count($data['count1'], $data['count2'], $new);
        $new->save();
    }

    // 处理姓名基数大小
    public function handler_name($surname, $name)
    {
        $count1 = $this->unique_rand(24, 59);
        $count2 = $this->unique_rand(0, 34);
        $rate1 = $this->rand_float();
        $rate2 = $this->rand_float();
        foreach ($this->com_surnames_list as $key => $com_surnames) {
            if (in_array($surname, $com_surnames)) {
                $count1 = $this->unique_rand((1276 - $key * 97), (2723 - $key * 134));
                $rate1 = $this->rand_float(6 - $key * 0.5, 12 - $key * 0.7);
            }
        }
        foreach ($this->com_names_list as $key => $com_names) {
            if (in_array($name, $com_names)) {
                $count2 = $this->unique_rand((878 - $key * 64), (1834 - $key * 91));
                $rate2 = $this->rand_float(6 - $key * 0.5, 12 - $key * 0.7);
            }
        }
        $return['count1'] = $count1;
        $return['count2'] = $count2;
        $return['rate'] = $rate1 + $rate2;
        return $return;
    }

    // 处理男女合并
    public function handler_count($count1, $count2, $new)
    {
        $return = array();
        for ($i = 0; $i < 10; $i++) {
            $count = $count1[$i] + $count2[$i];
            $male = rand(floor($count / 2), $count);
            $female = $count - $male;
            $res = $male . ',' . $female;
            array_push($return, $res);
        }
        $new->guangdong = $return[0];
        $new->jiangsu = $return[1];
        $new->chongqing = $return[2];
        $new->henan = $return[3];
        $new->tianjin = $return[4];
        $new->shanghai = $return[5];
        $new->anhui = $return[6];
        $new->shanxi = $return[7];
        $new->zhejiang = $return[8];
        $new->jilin = $return[9];
    }

    // 格式化数据
    public function format_res($data)
    {
        $res['list'] = array();
        $all_all = 0;
        $all_male = 0;
        $all_famale = 0;

        foreach ($data as $key => $item) {
            $i = explode(',', $item);
            $d = array(
                'province' => $this->get_chinese($key),
                'all' => $i[0] + $i[1],
                'male' => intval($i[0]),
                'female' => intval($i[1]),
            );
            $all_all += $i[0] + $i[1];
            $all_male += $i[0];
            $all_famale += $i[1];
            array_push($res['list'], $d);
        }

        $a = array(
            'province' => '汇总',
            'all' => $all_all,
            'male' => $all_male,
            'female' => $all_famale,
        );

        array_push($res['list'], $a);

        $res['all'] = $all_all;

        return $res;
    }

    // 获取省中文名
    public function get_chinese($key)
    {
        switch ($key) {
            case 'guangdong': return '广东';
            case 'jiangsu': return '江苏';
            case 'chongqing': return '重庆';
            case 'henan': return '河南';
            case 'tianjin': return '天津';
            case 'shanghai': return '上海';
            case 'anhui': return '安徽';
            case 'shanxi': return '山西';
            case 'zhejiang': return '浙江';
            case 'jilin': return '吉林';
        }
    }

    // 生成不重复数据(10个数字)
    public function unique_rand($min, $max) {
        //初始化变量为0
        $count = 0;
        //建一个新数组
        $return = array();
        while ($count < 10) {
            //在一定范围内随机生成一个数放入数组中
            $return[] = mt_rand($min, $max);
            //去除数组中的重复值用了“翻翻法”，就是用array_flip()把数组的key和value交换两次。这种做法比用 array_unique() 快得多。
            $return = array_flip(array_flip($return));
            //将数组的数量存入变量count中
            $count = count($return);
        }
        //为数组赋予新的键名
        shuffle($return);
        return $return;
    }

    // 生成随机浮点数
    public function rand_float($min = 0, $max = 1)
    {
        return $min + mt_rand() / mt_getrandmax() * ($max - $min);
    }

}
